//
//  visitorView.swift
//  swiftWeibo
//
//  Created by apple on 15/12/10.
//  Copyright © 2015年 张智伟. All rights reserved.
//

import UIKit

///访客视图 - 处理用户未登录的界面显示
class VisitorView: UIView {
    
    //MARK: - 设置视图信息
    func setupInfo(imageName: String?,title: String) {
         messageLabel.text = title
        
        guard let imgName = imageName else{
            startAnim()
            return
        }
        iconView.image = UIImage(named: imgName)
        //隐藏小房子
        homeIconView.hidden = true
        
//        maskIconView.hidden = true
        //将遮挡移到底层
        sendSubviewToBack(maskIconView)
    }
    
    //开启转轮动画
    private func startAnim(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 5
        anim.removedOnCompletion = false//用在不断重复的动画上
        //添加动画到图层
         iconView.layer.addAnimation(anim, forKey:  nil)
    }
    
    
    //MARK: - 构造函数
    //initwithFram 是UIView的制定构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        //导致用SB开发,调用这个函数会导致程序直接崩溃
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
//        setupUI()
    }
    //MARK: - 懒加载控件
    //转轮
    private lazy var iconView : UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    //遮罩图片
    private lazy var maskIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    //小房子
    private lazy var homeIconView : UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    //消息Lable 
    private lazy var messageLabel: UILabel = UILabel(title: "每天都有新鲜事儿值,快来看一看")
    //注册按钮
    lazy var registerButton: UIButton = UIButton(title: "注册", color: UIColor.yellowColor(), imageName: "common_button_white_disable")
    //登录按钮
    lazy var loginButton: UIButton = UIButton(title: "登录", color: UIColor.darkGrayColor(), imageName: "common_button_white_disable")

}

extension VisitorView{
    //设置界面
    private func setupUI(){
        //添加子控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        //translatesAutoresizingMaskIntoConstraints默认是true 支持使用setFram
        //false支持自动布局来设置控件
        for V in subviews{
            V.translatesAutoresizingMaskIntoConstraints = false
        }
//        iconView.translatesAutoresizingMaskIntoConstraints = false
        //转轮图标
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: 0))
        //遮罩图片
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[maskIconView]-0-|", options: [], metrics: nil, views: ["maskIconView": maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[maskIconView]-(regBtnHeight)-[registerButton]", options: [], metrics: ["regBtnHeight": -36], views: ["maskIconView": maskIconView, "registerButton": registerButton]))
        //小房子
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        //消息Label
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        //注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Left, relatedBy: .Equal, toItem: messageLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width , relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height , relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 25))
        //登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Right, relatedBy: .Equal, toItem: messageLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width , relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height , relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 25))
        //设置背景颜色 /灰度图white
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
    }
    
}